package com.b2.controller.web;

import com.b2.constant.JwtClaimsConstant;
import com.b2.pojo.dto.WebUserEditDto;
import com.b2.pojo.dto.WebUserLoginDto;
import com.b2.pojo.dto.WebUserRegister;
import com.b2.pojo.entity.WebUser;
import com.b2.pojo.vo.WebUserLoginVo;
import com.b2.pojo.vo.WebUserVo;
import com.b2.properties.JwtProperties;
import com.b2.result.Result;
import com.b2.service.WebUserService;
import com.b2.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/web/user")
@Api(tags = "网页用户相关接口")
@Slf4j
@CrossOrigin
public class WebUserController {

    @Autowired
    private WebUserService webUserService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/register")
    @ApiOperation("web端用户注册")
    public Result<Long> register(@RequestBody WebUserRegister webUserRegister) {
        return webUserService.register(webUserRegister);
    }

    @PostMapping("/login")
    @ApiOperation("web端用户登录")
    public Result<WebUserLoginVo> login(@RequestBody WebUserLoginDto webUserLogin) {
        WebUser userlogin = webUserService.login(webUserLogin);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.User_ID, userlogin.getId());
        claims.put(JwtClaimsConstant.USER_ROLE, userlogin.getUserRole());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        WebUserLoginVo userVo = new WebUserLoginVo();
        BeanUtils.copyProperties(userlogin, userVo);
        userVo.setToken(token);
        return Result.success(userVo);
    }

    /**
     * 查询所有用户信息
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询所有用户信息")
    public Result<List<WebUserVo>> selectAll() {
        List<WebUser> list = webUserService.list();
        List<WebUserVo> collect = list.stream().map(webUser -> {
            WebUserVo webUserVo = new WebUserVo();
            BeanUtils.copyProperties(webUser, webUserVo);
            return webUserVo;
        }).collect(Collectors.toList());

        return Result.success(collect);
    }

    /**
     * 修改web用户信息
     * @param webUserEditDto
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改web用户信息")
    public Result<Boolean> editWebUser(@RequestBody WebUserEditDto webUserEditDto) {
        return webUserService.editWebUser(webUserEditDto);
    }

    /**
     * 删除web用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("web端用户删除")
    public Result<Boolean> delete(@PathVariable Long id) {

        WebUser webUser = webUserService.getById(id);
        if (webUser == null) {
            return Result.error("用户不存在");
        }
        Integer userRole = webUser.getUserRole();
        if (userRole == 1) {
            return Result.error("用户不能删除");
        }
        boolean remove = webUserService.removeById(id);
        return Result.success(remove);
    }

}
